package cn.itcast.wushangxiantu.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FuBenChuangjian#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FuBenChuangjian extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private View view;

    private String mParam1;
    private String mParam2;

    private ViewHolder viewHolder;
    private Context context;

    public FuBenChuangjian(Context context) {
        // Required empty public constructor
        this.context = context;
    }

    public FuBenChuangjian() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FuBenChuangjian.
     */
    // TODO: Rename and change types and number of parameters
    public static FuBenChuangjian newInstance(String param1, String param2) {
        FuBenChuangjian fragment = new FuBenChuangjian();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_fu_ben_chuangjian, container, false);
        viewHolder = new ViewHolder(view);
        return view;
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    User user = (User) message.obj;
                    viewHolder.etName.setText("");
                    viewHolder.etPassword.setText("");
                    if (null != user) {
                        ToastUtil.toast(context, user.getStw());
                        EventBus.getDefault().post(new MessageEvent("getFuBenNum"));
                    } else {
                        ToastUtil.toast(context, "服务器炸了");
                    }
                    break;
            }
            return false;
        }
    });

    class ViewHolder {
        public View rootView;
        public EditText etName;
        public Spinner tvDiTu;
        public Spinner tvDj;
        public Spinner tvMenKan;
        public EditText etPassword;
        public Button btnOK;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.etName = (EditText) rootView.findViewById(R.id.etName);
            this.tvDiTu = (Spinner) rootView.findViewById(R.id.tvDiTu);
            this.tvDj = (Spinner) rootView.findViewById(R.id.tvDj);
            this.tvMenKan = (Spinner) rootView.findViewById(R.id.tvMenKan);
            this.etPassword = (EditText) rootView.findViewById(R.id.etPassword);
            this.btnOK = (Button) rootView.findViewById(R.id.btnOK);

            btnOK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (etName.getText().toString().trim().equals("")) {
                        ToastUtil.toast(context, "房间名不能为空");
                        return;
                    }
                    int dj = tvDj.getSelectedItemPosition();
                    int mindj = tvMenKan.getSelectedItemPosition();
                    int ditu = tvDiTu.getSelectedItemPosition();
                    CJfj(Login.getUser().getUsernumber(), etName.getText().toString().trim(),
                            etPassword.getText().toString().trim(), dj, mindj, ditu);

                }
            });
        }

        private void CJfj(String usernumber, String name, String password, int dj, int mindj, int ditu) {
            OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumber
                    + "&name=" + name + "&password=" + password + "&dj=" + dj + "&need=ChuangJianFangJian" + "&mindj=" + mindj + "&ditu=" + ditu, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.v("chuangjian1", e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String src = response.body().string();
                    Log.v("chuangjian2", src);
                    User user = null;
                    try {
                        user = new Gson().fromJson(src, User.class);
                    } catch (Exception e) {

                    }
                    Message message = new Message();
                    message.obj = user;
                    message.what = 1;
                    handler.sendMessage(message);
                }
            });
        }


    }
}